import threading
from queue import Queue
from threading import Thread
import time

class Producer(Thread):
    def __init__(self, name, queue):
        Thread.__init__(self, name=name)
        self.queue = queue

    def run(self):
        for i in range(1, 6):
            print(f'生产者线程{self.name}放入产品{i}')
            self.queue.put(i)
            time.sleep(1)
        print('生产者完成了所有数据的存放')

class Consumer(Thread):
    def __init__(self, name, queue):
        Thread.__init__(self, name=name)
        self.queue = queue
    def run(self):
        for _ in range(5):
            value = self.queue.get()
            print(f'消费者线程{self.name}取出产品{value}')
            time.sleep(1)
        print('---消费者线程完成了所有数据的取出----')

if __name__ == '__main__':
    queue = Queue()

    producer = Producer(name='producer', queue=queue)
    consumer = Consumer(name='consumer', queue=queue)
    producer.start()
    consumer.start()

    producer.join()
    consumer.join()
